using System;
using System.Collections.Generic;
using System.Text;
using Objects;

namespace DAL
{
    public class DALNguoiDung
    {
        public NguoiDung GetByUserPassword(string User, string Password)
        {
            NguoiDung ItemNguoiDung = new NguoiDung();
            clsDataAccess acc = new clsDataAccess();

            string[] sname = new string[] { "@TenDangNhap", "@MatKhau" };
            string[] vname = new string[] { User.ToString(), Password.ToString() };

            System.Data.DataSet ds = acc.CommandSelect("NguoiDung_GetByName", sname, vname);

            NguoiDung Item = new NguoiDung();

            if (ds.Tables[0].Rows.Count == 1)
            {
                System.Data.DataRow dr = ds.Tables[0].Rows[0];
                return Item.Populate(dr);
            }
            else
                return null;
        }

        public NguoiDung GeById(int Id)
        {
            clsDataAccess acc = new clsDataAccess();

            string[] sname = new string[] { "@Id" };
            string[] vname = new string[] { Id.ToString() };
            System.Data.DataSet ds = acc.CommandSelect("NguoiDung_GetById", sname, vname);

            NguoiDung Item = new NguoiDung();


            System.Data.DataRow dr = ds.Tables[0].Rows[0];
            return Item.Populate(dr);
        }

        public List<NguoiDung> GetAllItem()
        {
            List<NguoiDung> ListNguoiDung = new List<NguoiDung>();

            clsDataAccess acc = new clsDataAccess();

            string[] sname = new string[] { "@Status" };
            string[] vname = new string[] { Common.Myconst.HOATDONG.ToString() };
            System.Data.DataSet ds = acc.CommandSelect("NguoiDung_GetAllItem", sname, vname);

            NguoiDung Item = new NguoiDung();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                System.Data.DataRow dr = ds.Tables[0].Rows[i];
                ListNguoiDung.Add((Item.Populate(dr)));
            }
            return ListNguoiDung;
        }

        public int Insert_NguoiDung(String TenNguoiDung, String TenDangNhap, String MatKhau, DateTime NgayTao, DateTime LanDangNhapCuoi, int Quyen, int SapXep)
        {
            clsDataAccess acc = new clsDataAccess();

            string[] sname = new string[] { "@TenNguoiDung", "@TenDangNhap", "@MatKhau", "@NgayTao", "@LanDangNhapCuoi", "@Quyen", "@Status", "@SapXep" };
            string[] vname = new string[] { TenNguoiDung.ToString(), TenDangNhap.ToString(), MatKhau.ToString(), NgayTao.ToString(), LanDangNhapCuoi.ToString(), Quyen.ToString(), Common.Myconst.HOATDONG.ToString(), SapXep.ToString() };
            return acc.InsertUpdateDetete("NguoiDung_Insert", sname, vname);

        }

        public int Update_NguoiDung(int id, String TenNguoiDung, String username, String password, DateTime NgayTao, DateTime LanDangNhapCuoi, int Quyen, int SapXep)
        {
            clsDataAccess acc = new clsDataAccess();

            string[] sname = new string[] { "@Id", "@TenNguoiDung", "@TenDangNhap", "@MatKhau", "@NgayTao", "@LanDangNhapCuoi", "@Quyen", "@Status", "@SapXep" };
            string[] vname = new string[] { id.ToString(), TenNguoiDung.ToString(), username.ToString(), password.ToString(), NgayTao.ToString(), LanDangNhapCuoi.ToString(), Quyen.ToString(), Common.Myconst.HOATDONG.ToString(), SapXep.ToString() };
            return acc.InsertUpdateDetete("NguoiDung_Update", sname, vname);

        }

        public int Delete_NguoiDung(int id)
        {
            clsDataAccess acc = new clsDataAccess();

            string[] sname = new string[] { "@id" };
            string[] vname = new string[] { id.ToString() };
            return acc.InsertUpdateDetete("NguoiDung_Delete", sname, vname);

        }
    }
}
